#include <GL/glut.h>
#include <unistd.h>

static int time = 50;

static int angle = 0;
static bool flag = true;

void init()
{
    glClearColor(0, 0, 0, 0.0);
    glShadeModel(GL_SMOOTH);
}

void drawRedSqure()
{
    glBegin(GL_POLYGON);
    glColor3f(1, 0, 0);
    glVertex3f(0, 0, -20);
    glVertex3f(0, 2, -20);
    glVertex3f(1, 2, -20);
    glVertex3f(1, 0, -20);
    glEnd();
}

void drawGreenSqure()
{
    glBegin(GL_POLYGON);
    glColor3f(0, 1, 0);
    glVertex3f(0, 2, -20);
    glVertex3f(0, 4, -20);
    glVertex3f(1, 4, -20);
    glVertex3f(1, 2, -20);
    glEnd();
}

void drawBlueSqure()
{
    glBegin(GL_POLYGON);
    glColor3f(0, 0, 1);
    glVertex3f(0, 4, -20);
    glVertex3f(0, 6, -20);
    glVertex3f(1, 6, -20);
    glVertex3f(1, 4, -20);
    glEnd();
}

void drawThumb()
{
    glBegin(GL_POLYGON);
    glColor3f(1, 1, 0);
    glVertex3f(0, 0, -20);
    glVertex3f(-2, 0, -20);
    glVertex3f(-2, -1.5, -20);
    glVertex3f(0, -1.5, -20);
    glEnd();

    glBegin(GL_POLYGON);
    glColor3f(1, 0, 1);
    glVertex3f(-2, 0, -20);
    glVertex3f(-4, 0, -20);
    glVertex3f(-4, -1.5, -20);
    glVertex3f(-2, -1.5, -20);
    glEnd();
}

void drawMid()
{
    glBegin(GL_POLYGON);
    glColor3f(1, 1, 0);
    glVertex3f(0, 0, -20);
    glVertex3f(0, 2.3, -20);
    glVertex3f(1, 2.3, -20);
    glVertex3f(1, 0, -20);
    glEnd();

    glBegin(GL_POLYGON);
    glColor3f(1, 0, 1);
    glVertex3f(0, 2.3, -20);
    glVertex3f(0, 4.6, -20);
    glVertex3f(1, 4.6, -20);
    glVertex3f(1, 2.3, -20);
    glEnd();

    glBegin(GL_POLYGON);
    glColor3f(0, 1, 1);
    glVertex3f(0, 4.6, -20);
    glVertex3f(0, 7, -20);
    glVertex3f(1, 7, -20);
    glVertex3f(1, 4.6, -20);
    glEnd();
}

void drawFiger()
{
    // bottom
    glPushMatrix();
    glRotatef(angle, 0, 0, 1);
    drawRedSqure();
    glPopMatrix();

    // midile
    glPushMatrix();
    glRotatef(angle, 0, 0, 1);
    glTranslatef(0, 2, 0);
    glRotatef(angle, 0, 0, 1);
    glTranslatef(0, -2, 0);
    drawGreenSqure();
    glPopMatrix();

    // up
    glPushMatrix();
    glRotatef(angle, 0, 0, 1);
    glTranslatef(0, 2, 0);
    glRotatef(angle, 0, 0, 1);
    glTranslatef(0, -2, 0);
    glTranslatef(0, 4, 0);
    glRotatef(angle, 0, 0, 1);
    glTranslatef(0, -4, 0);
    drawBlueSqure();
    glPopMatrix();
}

void drawHand()
{
    glBegin(GL_POLYGON);
    glColor3f(1, 0, 1);
    glVertex3f(0, 0, -20);
    glVertex3f(0, -2, -20);
    glVertex3f(2, -2, -20);
    glVertex3f(2, 0, -20);
    glEnd();
}

void display()
{

    // 清除之前帧数据
    glClear(GL_COLOR_BUFFER_BIT);

    drawMid();
    drawFiger();
    drawThumb();
    drawHand();
    glFlush();
}

void myIdle(void)
{
    if (flag)
    {
        ++angle;
        if (angle == 50)
            flag = false;
    }
    else
    {
        --angle;
        if (angle == 0)
            flag = true;
    }

    display();
    usleep(100000);
}


void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.1, 100000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc, const char *argv[])
{
    // 初始化显示模式
    glutInit(&argc, const_cast<char **>(argv));
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

    // 初始化窗口
    glutInitWindowSize(800, 800);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);

    init();
    glutReshapeFunc(reshape);
    // glutDisplayFunc(display);
    glutIdleFunc(&myIdle);

    // 开始主循环绘制
    glutMainLoop();
    return 0;
}